/**
 * 
 */
package vn.support.connection.response.processor.impl;

import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import vn.support.connection.Connection;
import vn.support.connection.RequestConnection;
import vn.support.connection.response.processor.AbstractResponseProcessor;
import vn.support.core.dto.DataObject;
import vn.support.core.dto.SessionData;
import vn.support.system.SystemProperties;
import vn.support.ui.frame.AbstractLoginView;
import vn.support.ui.frame.ContactViewFrame;

/**
 * @author nttuyen
 *
 */
public class LoginProcessor extends AbstractResponseProcessor {

	private static final Logger log = Logger.getLogger(LoginProcessor.class);
	private final AbstractLoginView loginView;
    private volatile boolean enable = true;
	public LoginProcessor(AbstractLoginView view) {
        super();
        this.loginView = view;
        this.enable = true;
	}

    public void disable() {
        this.enable = false;
    }
	
    @Override
	public DataObject process(DataObject data) {
		String xml = data.get(Connection.DTO_RESPONSE_DATA);
        
        //Process exception
        Object obj = data.get(Connection.DTO_RESPONSE_EXCEPTION);
        if(obj != null) {
            if(this.enable){
                JOptionPane.showMessageDialog(null, SystemProperties.getValue("ui.login.error.title"), SystemProperties.getValue("ui.login.error.message"), JOptionPane.ERROR_MESSAGE);
                this.loginView.active();
            }
			return data;
        }

        //Loi tra ve ko fai dang XML
		if(!xml.startsWith("<?xml")) {
            log.debug("Response is not XML: " + xml);
            if(this.enable) {
                JOptionPane.showMessageDialog(null, SystemProperties.getValue("ui.login.error.title"), SystemProperties.getValue("ui.login.error.message"), JOptionPane.OK_OPTION);
                this.loginView.active();
            }
			return data;
		}

		SessionData sessionData = this.entityFactory.create(xml, "swiftwinapp", SessionData.class).iterator().next();
        DataObject system = SystemProperties.getData();
        SystemProperties.set(SystemProperties.SESSION, sessionData);
		system.set(SessionData.class.getName(), sessionData);
        
        try {
            if(!"".equals(sessionData.getSessionId())) {
                if(!this.enable) {
                    Connection connection = new RequestConnection();
                    DataObject logout = new DataObject(Connection.LOGOUT_ACTION);
                    logout.set("sessionid", sessionData.getSessionId());
                    connection.execute(logout);
                    
                    return data;
                }
                ContactViewFrame contact = new ContactViewFrame();
                SystemProperties.set(SystemProperties.MAIN_FRAME, contact);
                contact.setVisible(true);
                this.loginView.dispose();
            }
        } catch (Exception e) {
            log.error("Exception", e);
            JOptionPane.showMessageDialog(null, SystemProperties.getValue("ui.login.error.title"), SystemProperties.getValue("ui.login.error.message"), JOptionPane.ERROR_MESSAGE);
            this.loginView.active();
			return data;
        }
		return data;
	}
}
